const path = require('path');
const uploadService = require('../service/upload');
const UserService = require('../service/users');
const {app_port,app_host} = require('../app/config');
class UploadController {
  async createAvatar(ctx,next){
    console.log("createAvatar的控制器");
    const file = ctx.req.file;
    const userId = ctx.user.id;
    const {filename,size, mimetype} = file;
    const avatar = await uploadService.createAvatar(filename,size, mimetype,userId);
    const avatarUrl = `${app_host}:${app_port}/users/${userId}/avatar`
    //更新用户的头像
    const result= await UserService.updateAvatar(userId,avatarUrl);
    console.log(result);
    ctx.body = {
      code:200,
      data:{
        avatarUrl,
        result
      }
    }
  }
  async createPicture(ctx,next){
    console.log("createPicture的控制器");
    const files = ctx.req.files;
    const userId = ctx.user.id;
    const momentId = ctx.query.momentId
    console.log(momentId);
    for(let file of files){
      const {filename,size, mimetype} = file;
      const picture = await uploadService.createPicture(filename,size, mimetype,userId,momentId);
      console.log(picture);
    }
    ctx.body = {
      code:200,
      data:{
        message:"上传成功"
      }
    }
  }
}
module.exports = new UploadController();